<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Marko
 * Date: 5/18/13
 * Time: 4:43 PM
 * To change this template use File | Settings | File Templates.
 */

/*INSERT table(ip) VALUES (INET_ATON('192.168.0.1')); /*ip = 3232235521*/
/*SELECT INET_NTOA(ip) As IPAddress FROM table; /*IPAddress = 192.168.0.1*/


/* INFO
function return number of visits of specific post, and sets visit_count+1 if visitor is unique
*/

function get_set_count($user_ip, $post_id){
    // check if user visited this post
    $que = "SELECT * FROM visitors WHERE visitorIP = '". $user_ip . "' and posts_ID = " . $post_id;
    $res = mysql_query($que);
    if(mysql_num_rows($res) == 0)
    {
        $user_ip = "'" . $user_ip . "'";
        $que = "INSERT INTO visitors VALUES($user_ip, $post_id)";
        $que_update = "UPDATE posts SET visit_count = visit_count + 1 WHERE ID = $post_id";
        //echo $que . $que_update;
        $success = mysql_query($que);
            if($success)
                $success = mysql_query($que_update);
        if(!$success) die("Error loading post, please report to site admin");
    }
}

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
        //check ip from share internet
    {
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        //to check ip is pass from proxy
    {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
